package com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.itemdecoration
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 *@desc  自定义分割线
 *@author liuliheng
 *@time 2022/3/13  15:34
 **/
class GridLayoutDecoration(space: Int, spanCount: Int) : RecyclerView.ItemDecoration() {

    private val mSpace = space
    private val mSpanCount = spanCount

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val itemCount = parent.childCount
        for (i in 0.until(itemCount)) {
            when {
                i % mSpanCount == 0 -> {
                    outRect.left = mSpace
                    outRect.right = mSpace shr  1
                }

                i % mSpanCount == mSpanCount -1 -> {
                    outRect.left = mSpace shr  1
                    outRect.right = mSpace
                }

                else -> {
                    outRect.left = mSpace shr  1
                    outRect.right = mSpace shr  1
                }
            }
        }

        outRect.top = mSpace
    }

}